const PAGE_PATH = 'pages/index/index'
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
const isWeb = platformInfo.startsWith('web')
const isMP = platformInfo.startsWith('mp')

let page
const screenshotParams = {
  deviceShot: true,
  area: {
    x: 0,
    y: 0,
  }
}

describe('openDialogPage in onLaunch', () => {
  if (isMP) {
    it('not support mp', async () => {
      expect(true).toBeTruthy()
    })
    return
  }
    
  it('openDialogPage in onLaunch', async () => {
    if(isWeb){
      await new Promise((resolve) => {
        setTimeout(() => {
          resolve()
        }, 5000)
      })
    }
    page = await program.currentPage()
    await page.waitFor('view')
    await page.waitFor(1000)

    const windowInfo = await program.callUniMethod('getWindowInfo');
    screenshotParams.area.y = windowInfo.safeAreaInsets.top + 44

    let pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(5)

    const screenShotWithDialog = await program.screenshot(screenshotParams)
    expect(screenShotWithDialog).toSaveImageSnapshot();
    
    await page.callMethod('closeDialogPage')
    await page.waitFor(1000)
    
    pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(-3)
    
    const screenShotNoDialog = await program.screenshot(screenshotParams)
    expect(screenShotNoDialog).toSaveImageSnapshot();
  })
  
  it('openDialogPage in tabBar page', async () => {
    let pageLifeCycle = await page.callMethod('getLifeCycleNum')
    await page.callMethod('openDialogPage')
    await page.waitFor(1000)
    const dialogPageInTabBar = await program.screenshot(screenshotParams)
    pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(0)
    expect(dialogPageInTabBar).toSaveImageSnapshot();
    await page.callMethod('closeDialogPage')
    await page.waitFor(1000)
    pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(-8)
    const screenShotNoDialog = await program.screenshot(screenshotParams)
    expect(screenShotNoDialog).toSaveImageSnapshot();
  })

  it('reLaunch to index clear dialogPages', async () => {
    page = await program.navigateTo('/pages/setting/setting')
    await page.waitFor('view')
    await page.callMethod('openDialogPage')
    await page.waitFor(1000)
    let pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(-4)

    page = await program.reLaunch('/pages/index/index')
    await page.waitFor('view')
    pageLifeCycle = await page.callMethod('getLifeCycleNum')
    expect(pageLifeCycle).toEqual(-14)
  })
})